package com.duobaoyu.dby.supply.biz.statemachine;

import com.alibaba.cola.statemachine.Action;
import com.alibaba.cola.statemachine.Condition;
import com.alibaba.cola.statemachine.StateMachine;
import com.alibaba.cola.statemachine.builder.StateMachineBuilder;
import com.alibaba.cola.statemachine.builder.StateMachineBuilderFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName StateMachineBuilderConfig
 * @createTime 14:12:00 2023/10/23
 */
@Component
@RequiredArgsConstructor
public class StateMachineBuilderConfig {

    private final UserCloseAction userCloseAction;

    @Bean("orderOperaMachine")
    public StateMachine orderOperaMachine() {
        final String orderOpera = "order_opera";
        StateMachineBuilder<StatusEnum, OrderEvent, Order> builder = StateMachineBuilderFactory.create();
        //订单从初始化状态-待发货-状态-转到-关闭订单状态--用户关闭
        builder.externalTransitions()
                .fromAmong(StatusEnum.INIT, StatusEnum.WAITING_FOR_DELIVERY)
                .to(StatusEnum.CLOSE)
                .on(OrderEvent.USER_CLOSE)
                .when(checkCondition())
                .perform(userCloseAction);
        //订单从-初始化状态-已发货-待发货--转到-关闭订单状态--后台操作人员关闭
        builder.externalTransitions()
                .fromAmong(StatusEnum.INIT, StatusEnum.HAVE_BEEN_DELIVERY, StatusEnum.WAITING_FOR_DELIVERY)
                .to(StatusEnum.CLOSE)
                .on(OrderEvent.ADMIN_CLOSE)
                .when(checkCondition())
                .perform(doAction());
        //订单从等待发货状态-转为-订单关闭状态-超时关闭
        builder.externalTransition()
                .from(StatusEnum.WAITING_FOR_DELIVERY)
                .to(StatusEnum.CLOSE)
                .on(OrderEvent.OVERTIME_CLOSE)
                .when(checkCondition())
                .perform(doAction());
        //订单从待发货状态--转为-订单关闭状态-上级审批不通过关闭
        builder.externalTransition()
                .from(StatusEnum.WAITING_FOR_DELIVERY)
                .to(StatusEnum.CLOSE)
                .on(OrderEvent.CHECK_ERROR_CLOSE)
                .when(checkCondition())
                .perform(doAction());
        //订单从初始化状态--转为待发货状态--用户支付完毕动
        builder.externalTransition()
                .from(StatusEnum.INIT)
                .to(StatusEnum.WAITING_FOR_DELIVERY)
                .on(OrderEvent.USER_PAY)
                .when(checkCondition())
                .perform(doAction());

        StateMachine orderOperaMachine = builder.build(orderOpera);

        //打印uml图
        String plantUml = orderOperaMachine.generatePlantUML();
        System.out.println(plantUml);
        return orderOperaMachine;
    }

    private Condition<Order> checkCondition() {
        return (ctx) -> {
            return true;
        };
    }

    private Action<StatusEnum, OrderEvent, Order> doAction() {
        return (from, to, event, ctx) -> {
            System.out.println(ctx.getOrderName() + " 正在操作 " + ctx.getOrderId() + " from:" + from + " to:" + to + " on:" + event);
        };
    }

}
